package cn.tedu.autoshow.website.controller;

import static cn.tedu.autoshow.commons.constants.StatusConstants.*;
import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.website.pojo.param.TicketListAddNewParam;
import cn.tedu.autoshow.website.pojo.param.TicketListUpdaeInfoParam;
import cn.tedu.autoshow.website.pojo.vo.TicketListListItemVO;
import cn.tedu.autoshow.website.pojo.vo.TicketListStandardVO;
import cn.tedu.autoshow.website.service.ITicketListService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/ticketLists")
@Validated
@Slf4j
@Api(tags = "01. 票单列表管理模块")
public class TicketListController {
    @Autowired
    ITicketListService service;
    
    @PostMapping("/add-new")
    @ApiOperation("新增票单列表")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid TicketListAddNewParam cvsTicketListAddNewParam ,
                             @ApiIgnore HttpSession session){

        service.addNew(cvsTicketListAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除票单列表")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "票单列表ID", required = true, dataType = "Long")
    public JsonResult deleteById(
            @Range(min = 1, message = "票单列表ID不合法") @PathVariable Long id,
            @ApiIgnore HttpSession session){

        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID修改票单列表")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "票单列表ID", required = true, dataType = "Long")
    public JsonResult deleteById(
            @Range(min = 1, message = "票单列表ID不合法") @PathVariable Long id,
            @Valid TicketListUpdaeInfoParam cvsTicketListUpdaeInfoParam, @ApiIgnore HttpSession session){

        service.updateInfoById(id, cvsTicketListUpdaeInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/pay-paid")
    @ApiOperation("根据ID修改票单列表")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "票单列表ID", required = true, dataType = "Long")
    public JsonResult updatePayStatusByIdToRefund(
            @Range(min = 1, message = "票单列表ID不合法") @PathVariable Long id){

        service.setPayRefundById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/pay-refund")
    @ApiOperation("根据ID修改票单列表")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "票单列表ID", required = true, dataType = "Long")
    public JsonResult updatePayStatusByIdToRefunded(
            @Range(min = 1, message = "票单列表ID不合法") @PathVariable Long id){

        service.setPayRefundedById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/pay-refund-fail")
    @ApiOperation("根据ID修改票单列表")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "票单列表ID", required = true, dataType = "Long")
    public JsonResult updatePayStatusByIdToRefundFail(
            @Range(min = 1, message = "票单列表ID不合法") @PathVariable Long id){

        service.setPayRefundFailById(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询票单列表详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "票单列表ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询票单列表失败，请提交合法的ID值！")
            @RequestParam Long id){
        TicketListStandardVO cvsTicketListStandardVO = service.getStandardById(id);
        return JsonResult.ok(cvsTicketListStandardVO);
    }

    @GetMapping("/pay-refund")
    @ApiOperation("根据支付状态-待退款查询票单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")})
    public JsonResult listPayRefund(
            @Range(min = 1, message = "查询票单列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<TicketListListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.listByPatStatus(PAY_REFUND, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.listByPatStatus(PAY_REFUND, pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("/order/{id:[0-9]+}")
    @ApiOperation("根据订单Id查询票单列表")
    @ApiOperationSupport(order = 420)
    public JsonResult listPayRefund(
            @Range(min = 1, message = "票单列表ID不合法") @PathVariable Long id){
        List<TicketListListItemVO> listItemVOS = service.listByOrderId(id);
        return JsonResult.ok(listItemVOS);
    }

    @GetMapping("")
    @ApiOperation("查询票单列表列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")})
    public JsonResult list(
            @Range(min = 1, message = "查询相册列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<TicketListListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
